<?php
namespace Shop\Controller;
use Think\Controller;

class FundingController extends \Org\Util\AdminController {

    /**
     * model的中文名称
     */
    protected $modelName = '筹单';

    /**
     * model的icon
     */
    protected $modelIcon = 'icon-certificate';

    /**
     * 权限验证标志位,默认false
     * false:所有action都需要权限验证;
     * true:所有action都不需要权限验证;
     * array:array中的action不需要权限验证，其余需要。
     */
    protected $notAuthority = false;


    /**
     * 创建之后做的事情
     */
    public function interfaceAfterCreate($id) {
        //创建一个具体筹单
        $funding = D('Funding')->find($id);
        $fundingType = D('FundingType')->find($funding['funding_type'])['en'];
        $fundingType = 'Funding'.ucfirst($fundingType);
        unset($data);
        $data['principal_id'] = $id;
        D($fundingType)->create($data);
        $fundingSpecifyId = D($fundingType)->add();
        //通知管理员
        notice(session('商家')['id'], 1, '有新的筹单需要审核', '有新的筹单需要审核，请查看');
        //跳转到特定的筹单完善页面
        $this->redirect($fundingType.'/update?id='.$fundingSpecifyId);
    }

    /**
     * 更新之后做的事情
     */
    public function interfaceAfterUpdate($id) {
        //查找当前筹单的具体筹单，跳转到具体筹单进行完善
        $funding = D('Funding')->find($id);
        $fundingType = D('FundingType')->find($funding['funding_type'])['en'];
        $fundingType = 'Funding'.ucfirst($fundingType);
        $fundingSpecifyId = D($fundingType)->where(['principal_id'=>$id])->find()['id'];
        $this->redirect($fundingType.'/update?id='.$fundingSpecifyId);
    }

    /**
     * 删除之前做的事情
     */
    public function interfaceBeforeDelete($id) {
    }

    /**
     * 更新之前做的事情
     */
    public function interfaceBeforeUpdate($id) {
        $funding = D('Funding')->find($id);
        if ($funding['transaction_type'] == '购买') {
            $customizedTravel = D('CustomizedTravel')->where(['funding_id'=>$id])->find();
            if ($customizedTravel['state'] == '待发起') {
                //给卖家增加余额
                $user = D('User')->find($customizedTravel['club_user_id']);
                $newBalance = $user['balance']+C('BUY_FUNDING_PRICE');
                D('User')->where(['id'=>$customizedTravel['club_user_id']])->data(['balance'=>$newBalance])->save();
                //增加资金明细
                unset($data);
                $data['user_id'] = $customizedTravel['club_user_id'];
                $data['pay_type'] = '账户余额';
                $data['amount'] = C('BUY_FUNDING_PRICE');
                $data['remark'] = '购买筹单完成';
                $data['order_type'] = 'BuyFunding';
                $data['order_id'] = $customizedTravel['id'];
                D('Finance')->create($data);
                D('Finance')->add();

            }



            //把当前筹单修改为自己的
            D('Funding')->where(['id'=>$id])->data(['user_id'=>session('商家')['id'], 'transaction_type'=>'发起'])->save();
            //更新购买订单的状态
            D('CustomizedTravel')->where(['funding_id'=>$id])->data(['state'=>'已完成'])->save();
        }
    }

    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        parent::create();

    }

    public function delete() {
        parent::delete();
    }

    public function update($id) {
        parent::update($id);
    }

    public function list($type = '发起') {
        if($type == '代卖') {
            $_GET['searchMap']['state'] = '众筹中';
            $_GET['searchMap']['is_consignment'] = '是';
        } else {
            $_GET['searchMap']['user_id'] = session('商家')['id'];
        }
        parent::list();
    }

    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }

    public function tableOpeationButton() {
        $return  = '
                                <if condition="$_GET[\'type\'] neq \'代卖\'">
                                    <a class="" href="__MODULE__/FundingOrder/list?searchMap[funding_id]={$vo[name]}" >
                                        <button class="btn btn-xs btn-success" style="border: 1px;">
                                            <i class="icon-group"></i>
                                        </button>
                                    </a>
                                    <a class="" href="__MODULE__/Funding/update/id/{$vo[\'id\']}" >
                                        <button class="btn btn-xs btn-info" style="border: 1px;">
                                            <i class="icon-edit"></i>
                                        </button>
                                    </a>
                                <else/>
                                    <a class="" href="__MODULE__/Funding/consignment/id/{$vo[\'id\']}" onclick="return confirm(\'是否确认代卖该筹单?\');">
                                        <button class="btn btn-xs btn-success" style="border: 1px;">
                                            <i class="icon-check"></i>
                                        </button>
                                    </a>
                                </if>
                                    ';
        return $return;
    }

    public function consignment($id = 0) {
        //生成代卖二维码
        $url = 'http://www.116lyw.com/Home/Funding/detail/id/'.$id.'/consignment_id/'.session("商家")["id"];
        $qrcode = createQRcode('./Public/file/',$url,'H','10','qr_');
        //增加代卖记录
        unset($data);
        $data['user_id'] = session('商家')['id'];
        $data['funding_id'] = $id;
        $data['qrcode'] = $qrcode;
        D('FundingConsignment')->create($data);
        D('FundingConsignment')->add();
        $this->success('代卖成功!', U('FundingConsignment/list'));
    }

}